package com.zmh.service.timer;

import java.util.Date;

import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;

import com.zmh.common.DateUtil;

/**
 * 测试自定义定时器
 * @author zhoumh
 * @version 创建时间：2017年10月30日 上午11:05:52
 * @Description
 */
//@Service
//@EnableScheduling  
public class MyScheduler implements SchedulingConfigurer{
	private int i = 1;
	public String cron = "*/3 * * * * *";
//	@Scheduled(cron = "*/3 * * * * *")
	public void heartbeat() {
		// 任务逻辑
		System.out.println("这是一条测试信息："+DateUtil.getDateTimes());
	}
	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		
		taskRegistrar.addTriggerTask(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
//	            System.out.println("第"+(i++)+"次开始执行操作...  时间：【" + DateUtil.getDateTimes() +"】"); 
			}
		}, new Trigger() {
			
			@Override
			public Date nextExecutionTime(TriggerContext triggerContext) {
				// TODO Auto-generated method stub
                //任务触发，可修改任务的执行周期   
                CronTrigger trigger = new CronTrigger(cron);  
                Date nextExec = trigger.nextExecutionTime(triggerContext);  
                return nextExec;  
			}
		});
	}
}
